package com.bbs.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.websocket.server.PathParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.bbs.pojo.Post;
import com.bbs.service.PostService;

@Controller
public class PostController {
	@Autowired
	private PostService postService;
	
	/*
	 * 管理
	 */
	@RequestMapping("/toDesign")
	public String findPostAll(Model model){
		List<Post>postList=postService.findPostAll();
		model.addAttribute("postList", postList);
		return "views/design";
	}
	
	/*
	 * 根据id删除
	 */
	@RequestMapping("/deletePostById")
	public String deletePostById(@PathParam("id")String id){
		Integer ID= Integer.parseInt(id);
		postService.deletePostById(ID);
		return "forward:/toDesign";
	}

	/*
	 * 查看详细信息
	 */
	@RequestMapping("/tobloglist")
	public String bloglist(@PathParam("id")String id ,Model model){
		Integer ID= Integer.parseInt(id);
		Post post=postService.bloglist(ID);
		model.addAttribute("post", post);
		return "bloglist";
	}
	
	/*
	 * 根据title查询
	 */
	@RequestMapping("/titleSelect")
	public String selectPostByTitle(String title ,Model model){
		
		List<Post> postList=postService.selectBlogList(title);
		
		model.addAttribute("postList", postList);
		return "views/design";
	}
	
	/*
	 * 根据type查询
	 */
	@RequestMapping("/findPostByType")
	public String findPostByType(@PathParam("type")String type ,Model model){
		List<Post> postList=postService.findPostByType(type);
		model.addAttribute("postList", postList);
		return "views/design";
	}
	
	/*
	 * 添加
	 */
	
	@RequestMapping("/addPost")
	public String addPost(String title,String content,String type,String username,String userId) throws Exception{
		Date date=new Date();
		Post p=new Post();
		p.setTitle(title);
		p.setUsername(username);
		p.setContent(content);
		p.setType(type);
		p.setCreatTime(date);
		Integer id=Integer.parseInt(userId);
		p.setUserId(id);
		System.out.println(p);
		postService.addBlog(p);
		return "forward:/toDesign";
	}
//	/*
//	 * 批量删除
//	 * Deletion by Query
//	 */
	@RequestMapping("/deleteByQuery")
	public String deleteByQuery(@RequestParam(value="chechbox",defaultValue="0") String[] chechbox){
		System.out.println(chechbox.length);
		return "views/design";
		
	}
	
	
	
}
